var div = $('div.box4');
var text = div.find('a').text();
var mid = $('#mid');
var midc = $('#midc');
var max = 5;
var tries;
var id;
var index;

function Mid(){
	console.log('mid : '+tries);
	tries++;
    if(mid.val()=='' && tries < max)
        setTimeout(Mid, 2000);
    else
        self.postMessage(mid.val());
}

function Free(){
	if($('div.host').hasClass('free')){
		$('form.create').submit();
		return;
	}
	
	if($('div.host').hasClass('used')){
		index++;
	}
	
	$('#name').val(id+index);
	tries++;
	if(tries < max)
		setTimeout(Free, 2000);
}

self.on('message', function(data) {
	id=data.id;
	index=data.index;
	
	$('#tr_1000').click();
	tries = 0;
	Free();
});

tries = 0;
Mid();